package com.randao.manage.user.controller;


import com.randao.api.user.UserManageApi;
import com.randao.framework.common.api.CommonResult;
import com.randao.framework.common.api.ResultCode;
import com.randao.framework.model.domain.entity.Result;
import com.randao.framework.model.domain.user.UserInfo;
import com.randao.framework.utils.PhoneFormatCheckUtils;
import com.randao.manage.user.service.UserManagerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author： ~Mr·Weng~
 * @CreateTime： 2019年-04月-15日  17:42
 * Look at the wine in the glass toast the rich first.
 */
@RestController
@RequestMapping("/user")
public class UserManageController implements UserManageApi {

    @Autowired
    private UserManagerService userManagerService;  //用户管理

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;


    //创建短信验证码
    @Override
    @ApiOperation(value = "创建短信验证码")
    @PostMapping("/createSmsCode")
    public CommonResult createSmsCode(@RequestParam String phone) {

        try {
            //调用工具类,校验手机号
            if (!PhoneFormatCheckUtils.isPhoneLegal(phone)) {
                return CommonResult.failed("手机号格式错误");
            }
            String smsCode = userManagerService.createSmsCode(phone);
            return CommonResult.success(smsCode, "验证码发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCode.SERVER_ERROR);
        }
    }

    /**
     * 注册
     * @param uname
     * @param password
     * @param telephone
     * @param authCode
     * @return
     */
    @Override
    @ApiOperation(value = "注册功能")
    @PostMapping("/userRegister")
    public CommonResult userRegister(@RequestParam String uname,
                                     @RequestParam String password,
                                     @RequestParam String telephone,
                                     @RequestParam String authCode) {
        try {
            return userManagerService.userRegister(uname, password, telephone, authCode);

        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCode.SERVER_ERROR);
        }
    }



    /**
     * 登陆
     *
     * @param userInfo
     * @return
     */
    @Override
    @ApiOperation(value = "登陆以后返回tocken")
    @PostMapping("/loginUser")
    public CommonResult loginUser(@RequestBody UserInfo userInfo) {
        String tocken = null;
        try {
            tocken = userManagerService.loginUser(userInfo.getUsername(), userInfo.getPassword());

            if (tocken == null) {
                return CommonResult.parameterValidateFailed("用户名或密码错误");
            }

            HashMap<String, String> tokenMap = new HashMap<>();
            tokenMap.put("tocken", tocken);
            tokenMap.put("tokenHead", tokenHead);

            return CommonResult.success(tokenMap);

        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCode.SERVER_ERROR);
        }
    }


    /**
     * 刷新tocken
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "刷新tocken")
    @GetMapping("/refreshToken")
    public CommonResult refreshToken(HttpServletRequest request) {
        String oldTocken = request.getHeader(tokenHeader);
        String newTocken = null;
        try {
            newTocken = userManagerService.refreshTocken(oldTocken);

            if (newTocken == null) {
                return CommonResult.failed(ResultCode.NOT_LOGIN_UNAUTHORIZED);
            }
            Map<String, String> tockenMap = new HashMap<>();
            tockenMap.put("newTocken", newTocken);
            tockenMap.put("tockenHead", tokenHead);
            return CommonResult.success(tockenMap, "更新tocken成功");

        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCode.SERVER_ERROR);
        }
    }


    /**
     * 密码修改
     *
     * @param password
     */
    @Override
    public Result updatePassword(String password) {
        try {
            userManagerService.updatePassword(password);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    @Override
    @GetMapping("/findAll")
    public List<UserInfo> findAll() {
        return userManagerService.findAll();
    }
}
